{include file='public/header' /}
    <style>
        .layui-form-label{width: 140px}
        .layui-input-inline{}
        .header span{background: #009688;margin-left: 30px;padding: 10px;color:#ffffff;}
        .header div{border-bottom: solid 2px #009688;margin: 8px 0 6px 0;}
        .header button{float: right;margin-top: -5px;}
    </style>
</head>
<body style="padding: 10px;">
<div class="header">
    <span>添加租户</span>
    <div></div>
</div>
<form class="layui-form" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>选择业务员</label>
        <div class="layui-input-inline">
            <select name="sales_id" lay-filter="sales_id" value="" lay-verify="required" lay-search>
                <option value="">请选择</option>
                {$data['sales']|raw}
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>选择房屋</label>
        <div class="layui-input-inline">
            <select name="house_id" lay-filter="house_id" value="" lay-verify="required" lay-search>
                <option value="">请选择</option>
                {$data['house']|raw}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>选择房间号码</label>
        <div class="layui-input-inline">
            <select name="room_id" id="room_id" lay-filter="room_id" value="" lay-verify="required">
            </select>
        </div>
<!--        <div class="layui-form-mid layui-word-aux">如无房间请先添加房间</div>-->
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>租户名称:</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="tenant_name" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>租户手机号:</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="tenant_phone" lay-verify="required|phone">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>身份证:</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="tenant_certificates" lay-verify="required|identity">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>年龄:</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="tenant_age" lay-verify="required|number">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>性别：</label>
        <div class="layui-input-inline">
            <select name="tenant_sex" lay-filter="tenant_sex" value=""  lay-verify="required">
                <option value="">请选择</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>
    </div>




    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: #FF5722">*</span>身份证照片</label>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn layui-btn-sm" id="up">
                <i class="layui-icon">&#xe67c;</i>添加图片
            </button>
            <input type="hidden" name="tenant_idfrontimg" lay-verify="tenant_idfrontimg">
            <input type="hidden" name="id" value={$data['tenant_id']}>
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="save" class="layui-btn" lay-submit lay-filter="save">保存</button>
        </div>
    </div>
</form>

<script type="text/javascript">
    layui.use(['layer','form','upload'],function(){
        form = layui.form;
        layer = layui.layer;
        $ = layui.jquery;
        upload = layui.upload;
        upload.render({
            elem: '#up'
            ,url: '__PUBLIC__/admin/Tenant/upload'
            ,auto: true //选择文件后不自动上传
            ,bindAction: '#upup' //指向一个按钮触发上传
            ,multiple: true
            ,data: {
                id: function(){
                    //console.log($('input[name="name"]').val());
                    return $('input[name="id"]').val();
                }
            }
            ,done: function(res, index, upload){
                //获取当前触发上传的元素，一般用于 elem 绑定 class 的情况，注意：此乃 layui 2.1.0 新增
                var item = this.item;
                // console.log(item);
                // console.log(res);
                // console.log(index);
                // console.log(upload);
                layer.msg('保存成功<br>'+res.msg,{icon:1,time:1100});
                $("#up").after('<img src="'+res.msg+'" alt="" style="width: 200px">  ');
                $('input[name="tenant_idfrontimg"]').val($('input[name="tenant_idfrontimg"]').val()+'-'+res.msg);
            }
        });
        form.on('submit(save)', function(data){
            //console.log(data.elem.id); //得到checkbox原始DOM对象
            //console.log(data.elem.checked); //开关是否开启，true或者false
            // console.log(data.value); //开关value值，也可以通过data.elem.value得到
            // console.log(data.othis); //得到美化后的DOM对象
            var name = $.trim($('input[name="provid"]').val());
            var phone = $.trim($('input[name="phone"]').val());
            var id_card = $.trim($('input[name="id_card"]').val());
            var provid = $('select[name="provid"]').val();
            var cityid = $('select[name="cityid"]').val();
            var areaid = $('select[name="areaid"]').val();
            // console.log(data.elem);

            //.serialize()是将表单中的值序列化id=0&username=2&gid=1&pwd=2&truename=2
            var index = layer.msg('数据保存中', {icon: 16,time: 1000,shade : [0.5 , '#000' , true]});
            $.post('__PUBLIC__/admin/Tenant/save',$('form').serialize(),function(res){
                layer.close(index);
                if(res.code>0){
                    layer.alert(res.msg,{icon:2});
                }else{
                    layer.msg(res.msg);
                }
            },'json');return false;
        });
        form.on('select(house_id)', function(data){
            var index = layer.msg('房间加载中请稍候。。。', {icon: 16,time: 1000,shade : [0.5 , '#000' , true]});
            $.post('__PUBLIC__/admin/Tenant/room_id','house_id='+data.value,function(res){
                $("select[name='room_id'] option").remove();
                //console.log(res);
                if(res.code>0){
                    layer.alert(res.msg,{icon:2});
                    form.render('select');
                    //$(res.msg).insertAfter("#htmladd");
                    //renderForm();
                    //layer.alert(res.msg,{icon:2});
                }else{
                    $("#room_id").append(res.msg);
                    form.render('select');
                    layer.close(index);
                    layer.msg('查询成功');
                    // layer.msg('文字保存成功，正在提交图片请稍候。。。', {icon: 16,time: 100000,shade : [0.5 , '#000' , true]});
                    // var d1 = document.getElementById('upup');
                    // d1.click();
                    //setTimeout(function(){parent.window.location.reload();},1000);
                    //setTimeout(function(){window.location.reload();},1000);
                }
            },'json');
        });
        form.val("form", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值'con_house_nature' => string '1' (length=1)
            'sales_id':'{$data['sales_id']}'
            ,'house_id': '{$data['house_id']}'
            ,'room_id': '{$data['room_id']}'
            ,'tenant_name': '{$data['tenant_name']}'
            ,'tenant_phone': '{$data['tenant_phone']}'
            ,'tenant_certificates': '{$data['tenant_certificates']}'
            ,'tenant_age': '{$data['tenant_age']}'
            ,'tenant_sex': '{$data['tenant_sex']}'
            ,'tenant_idfrontimg': '{$data['tenant_idfrontimg']}'
            // ,'provid' :  '420000'
            // ,'cityid' :  '420100'
            // ,'areaid' :  '420115'
        });
        // form.verify({
        //     tenant_idfrontimg:[/\S/,'请上传图片']
        //     //我们既支持上述函数式的方式，也支持下述数组的形式
        //     //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        //     ,pass: [/^[\S]{6,12}$/,'密码必须6到12位，且不能出现空格']
        //
        // });
    });
    // window.onload=function(){
    //     form = layui.form;
    //     form.val("form", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值'con_house_nature' => string '1' (length=1)
    //         'room_id':'{$data['room_id']}'
    //     });
    // }

    // window.setInterval("ssave()",10000);//每隔1秒，调用一次getDateDemo()
    // function ssave(){
    //     console.log(navigator);
    // }
</script>
</body>
</html>